package it.polimi.swKnights.SWIMv2.ejb.helpRequest;

import it.polimi.swKnights.SWIMv2.ejb.authentication.LoginHandlerLocal;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.HelpRequest;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.RegisteredUser;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidDateException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.utility.DateUtility;

import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class HelpRequestHandler
 */
@Stateless
public class HelpRequestHandler implements HelpRequestHandlerRemote
{

	@PersistenceContext(unitName = "swimv2-manager")
	private EntityManager manager; // manager handlin the persistence data

	@EJB
	private LoginHandlerLocal loginController;

	@Override
	public void sendHelpRequest(String useridAsker, String password,
			String useridHelper, String title, String expirationDate,
			String summary) throws EntityNotFoundException, LoginFailException,
			InvalidLoginPrivilegesException, InvalidDateException
	{

		this.loginController.checkLoginDataAndPrivileges(useridAsker, password,
				RegisteredUser.class);
		this.loginController.checkBanned(useridAsker);

		Date endHelpDate = null;

		endHelpDate = DateUtility.getDate(expirationDate);
		DateUtility.checkAfterToday(endHelpDate);
		RegisteredUser helper = manager
				.find(RegisteredUser.class, useridHelper);

		RegisteredUser asker = manager.find(RegisteredUser.class, useridAsker);

		HelpRequest helpRequest = new HelpRequest(this.getID(asker, helper),
				asker, helper);

		helpRequest.setCreationDate(new Date());
		helpRequest.setEndDate(endHelpDate);

		helpRequest.setTitle(title);
		helpRequest.setSummary(summary);

		manager.persist(helpRequest);

	}

	private long getID(RegisteredUser asker, RegisteredUser helper)
	{

		String countQuery = "SELECT COUNT(h) FROM HelpRequest AS h "
				+ "WHERE h.askingUserName = :asker AND  h.answeringUserName = :helper";

		Query counter = this.manager.createQuery(countQuery);

		if (countQuery.contains(":asker"))
			counter.setParameter("asker", asker.getUserName());
		if (countQuery.contains(":helper"))
			counter.setParameter("helper", helper.getUserName());

		long count = (Long) counter.getSingleResult();

		if (count == 0)
		{
			return 0;
		}

		String idQuery = ""
				+ "SELECT MAX(h.id) FROM HelpRequest AS h "
				+ "WHERE h.askingUserName = :asker AND  h.answeringUserName = :helper";
		Query query = null;

		query = this.manager.createQuery(idQuery);

		if (idQuery.contains(":asker"))
			query.setParameter("asker", asker.getUserName());
		if (idQuery.contains(":helper"))
			query.setParameter("helper", helper.getUserName());

		Long newId = (Long) query.getSingleResult() + 1;

		return newId;

	}
}
